package com.distribution.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.distribution.common.exception.ServiceException;
import com.distribution.system.domain.entity.CmccDaOperatorFlow;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.mapper.CmccDaOperatorFlowMapper;
import com.distribution.system.service.CmccDaProductService;
import com.distribution.system.service.CmccDaOperatorFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service("cmccDaOperatorFlowService")
public class CmccDaOperatorFlowServiceImpl extends ServiceImpl<CmccDaOperatorFlowMapper, CmccDaOperatorFlow> implements CmccDaOperatorFlowService {

    @Autowired
    private CmccDaProductService cmccDaProductService;

    @Override
    public void addOperatorFlowInfo(Integer currentId, Integer operatorCardId) {
        //首先不能分流到自己
        if (Objects.equals(currentId, operatorCardId)) throw new ServiceException("无法分流到商品本身");

        //再者当前商品和分流商品必须存在
        CmccDaProduct currentCard = cmccDaProductService.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId, currentId),false);
        CmccDaProduct operatorFlowCard = cmccDaProductService.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId, operatorCardId),false);
        if (currentCard == null || operatorFlowCard == null)
            throw new ServiceException("当前商品或分流商品不存在");

        //其次确保当前分流信息不存在
        CmccDaOperatorFlow operatorFlow = getOne(new LambdaQueryWrapper<CmccDaOperatorFlow>().eq(CmccDaOperatorFlow::getProductId, currentId)
                .eq(CmccDaOperatorFlow::getOperatorFlowProductId, operatorCardId), false);
        if (operatorFlow != null) throw new ServiceException("当前分流信息已存在");

        //设置当前商品的分流开关打开
        currentCard.setOperatorFlowFlag("1");
        cmccDaProductService.updateById(currentCard);

        //再者当前商品与分流商品的基础佣金要一致
        /*if (!Objects.equals(currentCard.getBasicsCommission(), areaFlowCard.getBasicsCommission())){
            throw new ServiceException("当前商品与分流商品基础佣金不一致");
        }*/

        //添加绑定商品记录
        CmccDaOperatorFlow cmccDaOperatorFlow = new CmccDaOperatorFlow();
        cmccDaOperatorFlow.setProductId(currentId);
        cmccDaOperatorFlow.setProductName(currentCard.getProductName());
        cmccDaOperatorFlow.setOperatorFlowProductId(operatorCardId);
        cmccDaOperatorFlow.setOperatorFlowProductName(operatorFlowCard.getProductName());
        cmccDaOperatorFlow.setCreateTime(LocalDateTime.now());
        cmccDaOperatorFlow.setUpdateTime(LocalDateTime.now());
        save(cmccDaOperatorFlow);
    }

    @Override
    public List<CmccDaOperatorFlow> getOperatorFlowInfoListByProductId(Integer productId) {
        //根据当前商品id查询分流信息
        List<CmccDaOperatorFlow> cmccDaOperatorFlowList = list(new LambdaQueryWrapper<CmccDaOperatorFlow>().eq(CmccDaOperatorFlow::getProductId, productId));
        if (cmccDaOperatorFlowList == null || cmccDaOperatorFlowList.isEmpty()) throw new ServiceException("当前产品没有异网分流信息");
        return cmccDaOperatorFlowList;
    }
}
